package com.tencent.wxcloudrun.config;

import com.tencent.wxcloudrun.common.InsufficientPrivilegesException;
import com.tencent.wxcloudrun.common.LoginTimeOutException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

@Slf4j
@ControllerAdvice
public class HandlerException {

    @ExceptionHandler(Exception.class)
    @ResponseBody
    @ResponseStatus(HttpStatus.BAD_GATEWAY)
    public ErrorResult exceptionError (Exception e) {
        log.info("---------走到了统一错误异常处理----------");
        return ErrorResult.systemError(e.getMessage());
    }

    @ExceptionHandler(LoginTimeOutException.class)
    @ResponseBody
    @ResponseStatus(HttpStatus.UNAUTHORIZED)
    public ErrorResult loginTimeOutError (Exception e) {
        log.info("---------走到了统一登录超时异常处理----------");
        return ErrorResult.loginTimeOut(e.getMessage());
    }

    @ExceptionHandler(InsufficientPrivilegesException.class)
    @ResponseBody
    @ResponseStatus(HttpStatus.FORBIDDEN)
    public ErrorResult insufficientPrivilegesError (Exception e) {
        log.info("---------走到了统一权限不足异常处理----------");
        return ErrorResult.insufficientPrivileges(e.getMessage());
    }
}
